Page({
  data: {
    userInfo: {
      nickname: '',
      avatar: '',
      phone: '',
      email: '',
      realName: '',
      idNumber: '',
      gender: ''
    },
    genderOptions: [
      { label: '男', value: 'male' },
      { label: '女', value: 'female' },
      { label: '保密', value: 'secret' }
    ],
    genderIndex: 0,
    addressCount: 0
  },
  
  onLoad() {
    this.loadUserInfo()
  },
  
  onShow() {
    // 从其他页面返回时刷新数据
    this.loadUserInfo()
  },
  
  loadUserInfo() {
    // 从本地存储加载用户信息
    const userInfo = wx.getStorageSync('userInfo') || {
      nickname: '',
      avatar: '',
      phone: '',
      email: '',
      realName: '',
      idNumber: '',
      gender: ''
    }
    
    // 设置性别索引
    let genderIndex = 0
    if (userInfo.gender) {
      const index = this.data.genderOptions.findIndex(item => item.value === userInfo.gender)
      if (index !== -1) {
        genderIndex = index
      }
    }
    
    // 加载地址数量
    const addresses = wx.getStorageSync('addresses') || []
    
    this.setData({
      userInfo: {
        nickname: userInfo.nickname || '未设置',
        avatar: userInfo.avatar || '',
        phone: userInfo.phone || '',
        email: userInfo.email || '',
        realName: userInfo.realName || '',
        idNumber: userInfo.idNumber || '',
        gender: userInfo.gender || ''
      },
      genderIndex,
      addressCount: addresses.length
    })
    
    // 同步到全局数据
    const app = getApp()
    app.globalData.userInfo = this.data.userInfo
  },
  
  // 选择头像
  chooseAvatar() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0]
        this.updateAvatar(tempFilePath)
      },
      fail: (err) => {
        console.error('选择头像失败', err)
        wx.showToast({
          title: '选择头像失败',
          icon: 'none'
        })
      }
    })
  },
  
  // 更新头像
  updateAvatar(avatarUrl) {
    const userInfo = { ...this.data.userInfo, avatar: avatarUrl }
    this.setData({ userInfo })
    
    // 更新全局数据和本地存储
    const app = getApp()
    app.globalData.userInfo = userInfo
    wx.setStorageSync('userInfo', userInfo)
    
    wx.showToast({
      title: '头像已更新',
      icon: 'success'
    })
  },
  
  // 编辑昵称
  editNickname() {
    wx.navigateTo({
      url: '/pages/edit-nickname/edit-nickname'
    })
  },
  
  // 性别改变
  onGenderChange(e) {
    const index = parseInt(e.detail.value)
    const selectedGender = this.data.genderOptions[index]
    const userInfo = { ...this.data.userInfo, gender: selectedGender.value }
    
    this.setData({
      userInfo,
      genderIndex: index
    })
    
    // 更新全局数据和本地存储
    const app = getApp()
    app.globalData.userInfo = userInfo
    wx.setStorageSync('userInfo', userInfo)
    
    wx.showToast({
      title: '性别已更新',
      icon: 'success'
    })
  },
  
  bindPhone() {
    wx.navigateTo({
      url: '/pages/bind-phone/bind-phone'
    })
  },
  
  bindEmail() {
    wx.navigateTo({
      url: '/pages/bind-email/bind-email'
    })
  },
  
  realNameAuth() {
    wx.navigateTo({
      url: '/pages/real-name-auth/real-name-auth'
    })
  },
  
  // 地址管理
  goToAddressList() {
    wx.navigateTo({
      url: '/pages/address-list/address-list'
    })
  }
})
